/*
 * @Descripttion: none
 * @version: 0.1.0
 * @Author: ...
 * @Date: 2020-08-12 11:42:53
 * @LastEditors: zgw
 * @LastEditTime: 2020-08-13 13:37:02
 */
var express = require('express');
var router = express.Router();

var pool = require('../config');

//主界面
router.get('/', function (req, res) {
  res.render('search', {
    title: "search",
    status: 200,
    message: {
      data: []
    }
  })
});
//sql查询
router.get('/2', function (req, res) {
  let query = "select * from user order by id asc";
  let reqData = req.query;
  console.log(req.query);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, reqData.user, function (err, resault) {
      if (err) throw err;

      // res.send({
      //   results: resault
      // })

      res.render('search', {
        title: "search v2 table",
        data: resault
      })
    });
    //回收pool
    connection.release();
  });
});
//sql查询
router.get('/3', function (req, res) {
  let query = "select * from user order by id desc";
  let reqData = req.query;
  console.log(req.query);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, function (err, resault) {
      if (err) throw err;

      res.send({
        title: "search v2",
        data: resault
      })

    });
    //回收pool
    connection.release();
  });
});

//sql增加
router.post('/add', function (req, res) {
  let query = "insert into user (id,name,user,password) values (0 , ? , ? , ?)";
  let reqData = req.query;
  console.log(req.query);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, [reqData.name, reqData.user, reqData.password, ], function (err, resault) {
      if (err) throw err;

      res.send({
        title: "search add sueecss",
        data: resault
      })

    });
    //回收pool
    connection.release();
  });
});

//sql删除
router.post('/del', function (req, res) {
  let query = "delete from user where id = ?";
  let reqData = req.query;
  console.log(req.query);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, reqData.id, function (err, resault) {
      if (err) throw err;

      res.send({
        title: "search del sueecss",
        data: resault
      })

    });
    //回收pool
    connection.release();
  });
});

//sql更新
router.post('/update', function (req, res) {
  let query = "update user set name=? where id = ?";
  let reqData = req.query;
  console.log(req.query);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, [reqData.name, reqData.id], function (err, resault) {
      if (err) throw err;

      res.send({
        title: "search del sueecss",
        data: resault
      })

    });
    //回收pool
    connection.release();
  });
});

module.exports = router;